﻿using Caltech.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Caltech.Results;
using CaltechSoft.Service.CommandService;
using ServiceModel = CaltechSoft.Service.DBCommand;
using AutoMapper;
using CaltechSoft.Service.ReportService;
using CaltechSoft.Service.SearchCriteria;
using CaltechSoft.Service.Repositories;
using CaltechSoft.Service.EntityFW.Entity;
using System.Linq.Expressions;
using Caltech.Extensions;
using Caltech.Service.Common;

namespace Caltech.Controllers
{
    public class POController : BaseApiController<Po, PoModels, PoSearchModel>
    {
        public POController(IRepository<Po> repository)
            : base(repository)
        {
        }

        [HasAccess(Areas.PO | Areas.WO)]
        public override IHttpActionResult Get()
        {
            var queryItems = Request.RequestUri.ParseQueryString();
            string customerName = queryItems["customerName"];
            string poNumber = queryItems["poNumber"];
            int customerId;
            Expression<Func<Po, bool>> filter = null;

            if (int.TryParse(queryItems["customerId"], out customerId))
            {
                filter = po => po.Company.Id == customerId;
            }
            else if (!string.IsNullOrEmpty(customerName))
            {
                filter = po => po.Company.Name.ToLower().Contains(customerName.ToLower());
            }
            else if (!string.IsNullOrEmpty(poNumber))
            {
                filter = po => po.PoNumber.ToLower().Contains(poNumber.ToLower());
            }

            return QueryData(filter);
        }
    }


}
